package py.com.alfa.escriba.gui.exception;

import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;

import py.com.alfa.escriba.gui.config.EscribaGUIApplicationContextProvider;
import py.com.sif.rest.client.spring.exception.RestClientException;

public class ExceptionHandler {
	

	/*public static String handle(Exception e, Locale locale, Class<?> clazz){
		Logger logger = LoggerFactory.getLogger(clazz);
		logger.error(e.getMessage(), e);
		return handle(e,locale);
	}
	
	public static String handle(Exception e, Locale locale){
		ApplicationContext ctx = EscribaGUIApplicationContextProvider.getContext();
		String message = ctx.getMessage("sistema.error", null, locale);
		Throwable tr = e.getCause();
		while(tr!=null) {
			if(tr instanceof RestClientException){
				message = tr.getMessage();
				break;
			}
			tr= tr.getCause();
		}
		return message;	
	}*/
	
	public static String handle(Exception e, Locale locale, Class<?> clazz) {
		Logger logger = LoggerFactory.getLogger(clazz);
		logger.error(e.getMessage(), e);
		return handle(e, locale);
	}

	public static String handle(Exception e, Locale locale) {
		ApplicationContext ctx = EscribaGUIApplicationContextProvider.getContext();
		String message = ctx.getMessage("sistema.error", null, locale);
		if (e.getCause() != null && e.getCause() instanceof RestClientException) {
			message = e.getCause().getMessage();
		} else if (e.getCause() != null && e.getCause() instanceof ServiceException) {
			message = e.getCause().getMessage();
		} else if (e.getMessage() != null) {
			message = e.getMessage();
		}
		return message;
	}

}
